package com.itheima.reggie.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;
    //根据菜品id回显 确定套餐是被启用的  采用redis
    @GetMapping("/dish/list")
    public  ResultInfo getListApp(Long categoryId, @RequestParam(defaultValue = "1") Integer status){
        //创建redis见
        String key="dish_"+categoryId;
        //通过建得到菜品集合
        List<Dish> dishList =(List<Dish>) redisTemplate.opsForValue().get(key);
        //判断redis中是否缓存有菜品
        if(CollectionUtil.isNotEmpty(dishList)){
            //返回菜品数组
            return ResultInfo.success(dishList);
        }else {
            //如果redis不存在 调用service查询
            dishList= dishService.getListApp(categoryId,status);
            //将查询的菜品保存到redis中
            redisTemplate.opsForValue().set(key,dishList);
            //返回
            return ResultInfo.success(dishList);

        }
    }
}
